package service

import (
	"goseckill/dao"
	"goseckill/models"
)

// Service 接口
type IOrderService interface {
	GetOrderByID(id int64)(order *models.Order, err error)
	DeleteByID(id int64) bool
	// 改
	UpdateOrder(order *models.Order) (err error)
	// 增
	InsertOrder(order *models.Order)(orderID int64, err error)
	// 查
	GetAllOrder() (orders []*models.Order, err error)
	// 联表查询
	GetAllOrderInfo() (orderMap map[int]map[string]string,err error)
	//
	InsertOrderByMessage(msg *models.Message)(orderID int64, err error)
}

// serviceImpl
type OrderService struct{
	OrderDao dao.IOrderDao
}

// New
func NewOrderService(dao dao.IOrderDao) IOrderService {
	return &OrderService{dao}
}

// 实现接口
func (o *OrderService) GetOrderByID(id int64)(order *models.Order, err error){
	return o.OrderDao.SelectByKey(id)
}
// 删除
func (o *OrderService) DeleteByID(id int64) bool {
	return o.OrderDao.Delete(id)
}
// 改
func (o *OrderService) UpdateOrder(order *models.Order) (err error) {
	return o.OrderDao.Update(order)
}
// 增
func (o *OrderService) InsertOrder(order *models.Order)(orderID int64, err error) {
	return o.OrderDao.Insert(order)
}
// 查
func (o *OrderService) GetAllOrder() (orders []*models.Order, err error){
	return o.OrderDao.SelectAll()
}
// 联表查询
func(o *OrderService)  GetAllOrderInfo() (orderMap map[int]map[string]string,err error){
	return o.OrderDao.SelectAllWithInfo()
}
//
func(o *OrderService)  InsertOrderByMessage(msg *models.Message)(orderID int64, err error){
	order := &models.Order{
		UserId: msg.UserID,
		ProductId: msg.ProductID,
		OrderStatus: models.OrderSuccess,
	}
	return o.InsertOrder(order)
}